use std::time::Duration;
use rumqttc::{AsyncClient, MqttOptions, QoS};
use tokio::{task, time};

// 测试的百度开源mqtt broker: https://bifromq.io/
#[tokio::main]
async fn main() {
    let mut mqttoptions = MqttOptions::new("rumqtt-async", "192.168.0.111", 1883);
    mqttoptions.set_keep_alive(Duration::from_secs(5));

    let (mut client, mut eventloop) = AsyncClient::new(mqttoptions, 10);
    client.subscribe("$iot/device1/user/fortest", QoS::AtMostOnce).await.unwrap();

    task::spawn(async move {
        for i in 0..10 {
            client.publish("$iot/device1/user/fortest", QoS::AtLeastOnce, false, format!("abcd-{}", i)).await.unwrap();
            time::sleep(Duration::from_millis(100)).await;
        }
    });

    while let Ok(notification) = eventloop.poll().await {
        println!("Received = {:?}", notification);
    }
}
